package com.cn.kehong.service;

import com.cn.kehong.api.ISysItemService;
import com.cn.kehong.dao.SysDeviceMapper;
import com.cn.kehong.dao.SysItemMapper;
import com.cn.kehong.domain.SysDevice;
import com.cn.kehong.domain.SysDeviceExample;
import com.cn.kehong.domain.SysItem;
import com.cn.kehong.domain.SysItemExample;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @类名:SysItemServiceImpl
 * @包名:com.cn.device.service
 * @作者:chenxingliang
 * @时间:2022-11-23 13:38
 * @版本:1.0.0
 */
@Service
public class SysItemServiceImpl implements ISysItemService {

    @Autowired
    private SysItemMapper sysItemMapper;
    @Autowired
    private SysDeviceMapper sysDeviceMapper;
    @Override
    public List<SysItem> getMgItemList(QueryForm queryForm) throws Exception {
        List<SysItem> sysItems = sysItemMapper.selectSysItems(queryForm);
        for (SysItem sysItem:sysItems){
            if (queryForm.getItemType().equals("7")){
                String nameStr= sysItem.getItemName()+"-"+sysItem.getItemCode();
                sysItem.setNameStr(nameStr);
            }else {
                String nameStr= sysItem.getItemName()+"-"+sysItem.getItemAlias();
                sysItem.setNameStr(nameStr);
            }
        }
        if (queryForm.getItemType().equals("9")){
            sysItems.sort(Comparator.comparing(SysItem::getItemTitle));
        }
        return sysItems;
    }

    @Override
    public List<SysItem> getMgItemSelect(QueryForm queryForm) throws Exception {
        List<String> types = queryForm.getTypes();
        if(CollectionsUtil.isEmpty(types)){
            return new ArrayList<>();
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeIn(types).andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("item_name asc");
        List<SysItem> list = sysItemMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<SysItem> getMgItemByType(String type) throws Exception {
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo(type).andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("item_name asc");
        return sysItemMapper.selectByExample(example);
    }



    @Override
    public List<SysItem> getAllMgItemList() throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        List<SysItem> list = sysItemMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<SysItem> getConcatUsers() throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS).andItemTypeEqualTo("16");
        List<SysItem> list = sysItemMapper.selectByExample(example);
        return list;
    }

    @Override
    public Map<String, String> getMgItemNameMap(QueryForm queryForm) throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(queryForm.getType())){
            criteria.andItemTypeEqualTo(queryForm.getType());
        }
        List<String> types = queryForm.getTypes();
        if(CollectionsUtil.isNotEmpty(types)){
            criteria.andItemTypeIn(types);
        }
        criteria.andItemStatusEqualTo(Constants.NORMAL_STATUS);
        List<SysItem> list = sysItemMapper.selectByExample(example);
        Map<String,String> result = Maps.newHashMap();
        for(SysItem mgItem:list){
            result.put(mgItem.getId(),mgItem.getItemName());
        }
        return result;
    }

    @Override
    public String addMgItemDetail(SysItem mgItem) throws Exception {
        String itemName = mgItem.getItemName();
        String itemType = mgItem.getItemType();
        if(!StringUtils.equals(mgItem.getRepeat(),"yes")){
            SysItemExample example = new SysItemExample();
            example.createCriteria().andItemTypeEqualTo(itemType).andItemNameEqualTo(itemName).andStatusEqualTo(Constants.NORMAL_STATUS);
            int existCount = sysItemMapper.countByExample(example);
            if(existCount > 0){
                return "名称重复！";
            }
        }
        mgItem.setId(CommonUtil.generateRandomNum("item-"));
        int count = sysItemMapper.insertSelective(mgItem);
        return CommonUtil.outStr(count);
    }

    @Override
    public String getMgItemDefaultValue(SysItem mgItem) throws Exception {
        String itemType = mgItem.getItemType();
        String itemName = mgItem.getItemName();
        if(StringUtils.isEmpty(itemType)|| StringUtils.isEmpty(itemName)){
            return "";
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo(itemType).andItemNameEqualTo(itemName).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<SysItem> list = sysItemMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)||list.size()!=1){
            return "";
        }
        return list.get(0).getId();
    }

    @Override
    public int countMgItemByName(String name, String type, String id) throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andItemNameEqualTo(name).andItemTypeEqualTo(type);
        if(StringUtils.isNotBlank(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = sysItemMapper.countByExample(example);
        return count;
    }

    @Override
    public int countMgItemByName1(String name,String alias, String type, String id) throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andItemAliasEqualTo(alias).andItemNameEqualTo(name).andItemTypeEqualTo(type);
//        if (StringUtils.isNotBlank(price)){
//            criteria.andItemPriceEqualTo(price);
//        }
        if(StringUtils.isNotBlank(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = sysItemMapper.countByExample(example);
        return count;
    }

    @Override
    public String getReferenceByWorkshopLine(String workshop, String lineBody) throws Exception {
        //workshop是15-2的格式需要拆分
        String[] parts = workshop.split("-");
        //厂房
        String factory = parts[0];
        //车间
        String shop = parts[1];

        SysItemExample itemExample = new SysItemExample();
        itemExample.createCriteria().andItemNameEqualTo(factory).andItemAliasEqualTo(shop).andItemCodeEqualTo(lineBody).andItemTypeEqualTo("7").andItemStatusEqualTo("0");
        List<SysItem> sysItems = sysItemMapper.selectByExample(itemExample);
        String priceSumStr="";
        if (sysItems.size()>0){
             priceSumStr = String.valueOf(sysItems.get(0).getItemPrice());
        }
        return priceSumStr;
    }

    @Override
    public String addMgItemBasicDetail(SysItem mgItem) {
        //仓房
        String itemName = mgItem.getItemName();
        //类型(7.基准节拍)
        String itemType = mgItem.getItemType();
//        String itemAlias = mgItem.getItemAlias();
        //车间
        String itemCode = mgItem.getItemCode();
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo(itemType).andItemNameEqualTo(itemName).andItemCodeEqualTo(itemCode).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = sysItemMapper.countByExample(example);
            if(existCount > 0){
                return "该仓房车间已存在基准节拍！";
            }
         //通过仓房车间查询有多少条线体
        SysDeviceExample sysDeviceExample = new SysDeviceExample();
        sysDeviceExample.createCriteria().andFactoryEqualTo(itemName).andWorkshopEqualTo(itemCode).andStatusEqualTo("0");
        int countByExample = sysDeviceMapper.countByExample(sysDeviceExample);
        if (countByExample == 0){
            mgItem.setItemAlias("0");
        }else {
            BigDecimal decimal = new BigDecimal(mgItem.getItemPrice());
            //计算车间综合基准节拍
            BigDecimal omprehensive = decimal.divide(BigDecimal.valueOf(countByExample),3, BigDecimal.ROUND_HALF_UP);
            //把omprehensive赛到item_alias中
            mgItem.setItemAlias(String.valueOf(omprehensive));
        }


        mgItem.setId(CommonUtil.generateRandomNum("item-"));
        int count = sysItemMapper.insertSelective(mgItem);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateMgItemBasicDetail(SysItem mgItem) throws Exception{
        String id = mgItem.getId();
        if(StringUtils.isBlank(id)){
            return "updateMgItemDetail id null";
        }
        String itemName = mgItem.getItemName();
        String itemType = mgItem.getItemType();
        //车间
        String itemCode = mgItem.getItemCode();
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo(itemType).andItemNameEqualTo(itemName).andItemCodeEqualTo(itemCode).andStatusEqualTo(Constants.NORMAL_STATUS);
//        int existCount = sysItemMapper.countByExample(example);
//        if(existCount > 0){
//            return "该仓房车间已存在基准节拍！";
//        }
        //通过仓房车间查询有多少条线体
        SysDeviceExample sysDeviceExample = new SysDeviceExample();
        sysDeviceExample.createCriteria().andFactoryEqualTo(itemName).andWorkshopEqualTo(itemCode).andStatusEqualTo("0");
        int countByExample = sysDeviceMapper.countByExample(sysDeviceExample);
        if (countByExample==0){
            mgItem.setItemAlias("0");
        }else {
            BigDecimal decimal = new BigDecimal(mgItem.getItemPrice());
            //计算车间综合基准节拍
            BigDecimal omprehensive = decimal.divide(BigDecimal.valueOf(countByExample),3, BigDecimal.ROUND_HALF_UP);
            //把omprehensive赛到item_alias中
            mgItem.setItemAlias(String.valueOf(omprehensive));
        }
        int count = sysItemMapper.updateByExampleSelective(mgItem,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String getReferenceByWorkshopBasic(String workshop,String type) throws Exception {
        //workshop是15-2的格式需要拆分
        String[] parts = workshop.split("-");
        //厂房
        String factory = parts[0];
        //车间
        String shop = parts[1];

        SysItemExample itemExample = new SysItemExample();
        itemExample.createCriteria().andItemNameEqualTo(factory).andItemCodeEqualTo(shop).andItemTypeEqualTo("7").andItemStatusEqualTo("0");
        List<SysItem> sysItems = sysItemMapper.selectByExample(itemExample);
        String priceSumStr="";
        String workShopSumStr="";
        if (sysItems.size()>0){
            priceSumStr = String.valueOf(sysItems.get(0).getItemPrice());
            workShopSumStr=String.valueOf(sysItems.get(0).getItemAlias());
        }
        if (type.equals("0")){
            return priceSumStr;
        }else {
            return workShopSumStr;
        }

    }

    @Override
    public String addRawMaterial(List<SysItem> sysItems) throws Exception {
        List<String> sysItemlist = Lists.newArrayList();
        for (SysItem sysItem:sysItems){
            String result = this.sysItemLegal(sysItem);
            //判断修改的物料数据是否满足字段的必填条件
            if (!"".equals(result)){
                return result;
            }

            String sysItemId = CommonUtil.generateRandomNum("item-");
            sysItem.setId(sysItemId);
            sysItem.setItemType("8");
            sysItem.setStatus("0");
            sysItem.setCreateTime(new Date());
            sysItemlist.add(sysItem.getItemCode());
        }

        SysItemExample example = new SysItemExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andItemTypeEqualTo("8").andItemCodeIn(sysItemlist);
        int existsCount = sysItemMapper.countByExample(example);
        if (existsCount > 0){
            return "物料编码已存在！";
        }

        int count = sysItemMapper.batchInsertsysItems(sysItems);

        return CommonUtil.outStr(count);
    }

    @Override
    public String updateRawMaterial(SysItem sysItem) throws Exception {
        String id = sysItem.getId();

        if (StringUtils.isEmpty(id)){
            return "id is null";
        }

        String result = this.sysItemLegal(sysItem);

        //判断修改的数据是否满足字段的必填条件
        if (StringUtils.isNotEmpty(result)){
            return result;
        }

        if (this.countCode(sysItem.getItemCode(),sysItem.getItemType(),id) > 0){
            return "物料编码已存在";
        }

        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id).andStatusEqualTo(Constants.NORMAL_STATUS);

        int count = sysItemMapper.updateByExampleSelective(sysItem,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public String addProduct(SysItem mgItem) throws Exception {
        String itemCode = mgItem.getItemCode();
        if (!mgItem.getItemCode().matches("\\d{10}")) {
            return "物料编码必须是10位数字";
        }
            SysItemExample example = new SysItemExample();
            example.createCriteria().andItemCodeEqualTo(itemCode).andItemTypeEqualTo(mgItem.getItemType()).andStatusEqualTo(Constants.NORMAL_STATUS);
            int existCount = sysItemMapper.countByExample(example);
            if(existCount > 0){
                return "物料编码已存在！";
            }
//            if (StringUtils.isNotEmpty(mgItem.getItemAlias())){
//                example.createCriteria().andItemAliasEqualTo(mgItem.getItemTitle());
//                int itemAlias=sysItemMapper.countByExample(example);
//                if(itemAlias > 0){
//                    return "该物料编码下的型号已存在！";
//                }
//            }
//            if (StringUtils.isNotEmpty(mgItem.getItemTitle())){
//                example.createCriteria().andItemTitleEqualTo(mgItem.getItemTitle());
//                int itemTitle=sysItemMapper.countByExample(example);
//                if(itemTitle > 0){
//                    return "该物料编码下的规格已存在！";
//                }
//            }
        mgItem.setId(CommonUtil.generateRandomNum("item-"));
        int count = sysItemMapper.insertSelective(mgItem);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateProduct(SysItem sysItem) throws Exception {
        String id = sysItem.getId();

        if (StringUtils.isEmpty(id)){
            return "id is null";
        }

        String result = this.sysItemProduct(sysItem);

        //判断修改的数据是否满足字段的必填条件
        if (StringUtils.isNotEmpty(result)){
            return result;
        }

        if (this.countCode(sysItem.getItemCode(),sysItem.getItemType(),id) > 0){
            return "物料编码已存在";
        }

        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id).andStatusEqualTo(Constants.NORMAL_STATUS);

        int count = sysItemMapper.updateByExampleSelective(sysItem,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public String addWorkshopDetail(SysItem mgItem) throws Exception {
        String itemName = mgItem.getItemName();
        String itemType = mgItem.getItemType();
        if(!StringUtils.equals(mgItem.getRepeat(),"yes")){
            SysItemExample example = new SysItemExample();
            example.createCriteria().andItemTypeEqualTo(itemType).andItemNameEqualTo(itemName).andItemAliasEqualTo(mgItem.getItemAlias()).andStatusEqualTo(Constants.NORMAL_STATUS);
            int existCount = sysItemMapper.countByExample(example);
            if(existCount > 0){
                return "该厂房车间已存在！";
            }
        }
        mgItem.setId(CommonUtil.generateRandomNum("item-"));
        int count = sysItemMapper.insertSelective(mgItem);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<SysItem> getMaterialCode() throws Exception {
        SysItemExample sysItemExample = new SysItemExample();
        sysItemExample.createCriteria().andItemTypeEqualTo("8").andItemStatusEqualTo("0").andStatusEqualTo("0");
        List<SysItem> sysItems = sysItemMapper.selectByExample(sysItemExample);
        return  sysItems;
    }

    @Override
    public List<SysItem> getMaterialTitle() throws Exception {
        SysItemExample sysItemExample = new SysItemExample();
        sysItemExample.createCriteria().andItemTypeEqualTo("10").andItemStatusEqualTo("0").andStatusEqualTo("0");
        List<SysItem> sysItems = sysItemMapper.selectByExample(sysItemExample);
        return  sysItems;
    }

    @Override
    public List<SysItem> getMgItemListFactoryName(QueryForm queryForm) throws Exception {
        List<SysItem> sysItems = sysItemMapper.getMgItemListFactoryName(queryForm);

        return sysItems;
    }

    @Override
    public int countMgItemByCode(String code, String type, String id) throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andItemCodeEqualTo(code).andItemTypeEqualTo(type);
        if(StringUtils.isNotBlank(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = sysItemMapper.countByExample(example);
        return count;
    }

    @Override
    public String updateMgItemDetail(SysItem mgItem) throws Exception {
        String id = mgItem.getId();
        if(StringUtils.isBlank(id)){
            return "updateMgItemDetail id null";
        }
        String itemName = mgItem.getItemName();
        String itemType = mgItem.getItemType();
        if(this.countMgItemByName(itemName,itemType,id) > 0){
            return "名称重复！";
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id);
        int count = sysItemMapper.updateByExampleSelective(mgItem,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateMgItemCodeDetail(SysItem mgItem) throws Exception {
        String id = mgItem.getId();
        if(StringUtils.isBlank(id)){
            return "updateMgItemCodeDetail id null";
        }
        String itemCode = mgItem.getItemCode();
        String itemType = mgItem.getItemType();
        if(this.countMgItemByCode(itemCode,itemType,id) > 0){
            return "挤塑机号重复！";
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id);
        int count = sysItemMapper.updateByExampleSelective(mgItem,example);
        return CommonUtil.outStr(count);
    }


    @Override
    public String updateMgItemValue(SysItem mgItem) throws Exception {
        String id = mgItem.getId();
        if(StringUtils.isBlank(id)){
            return "updateMgItemValue id null";
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id);
        int count = sysItemMapper.updateByExampleSelective(mgItem,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteMgItemDetail(QueryForm queryForm) throws Exception {
        List<String> ids = queryForm.getIds();
        if(CollectionsUtil.isEmpty(ids)){
            throw new Exception("deleteMgItemDetail ids 为空！");
        }
        SysItem update = new SysItem();
        update.setStatus(Constants.DEL_STATUS);
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdIn(ids);
        int count = sysItemMapper.updateByExampleSelective(update,example);
        return  CommonUtil.outStr(count);
    }

    @Override
    public SysItem getSysItemInfoById(String id) throws Exception {
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<SysItem> systems = sysItemMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(systems)||systems.size() != 1){
            return null;
        }
        return systems.get(0);
    }

    @Override
    public String addSysItemByNotRepeat(SysItem mgItem) throws Exception {
        String itemName = mgItem.getItemName();
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemNameEqualTo(itemName).andStatusEqualTo(Constants.NORMAL_STATUS).andItemTypeEqualTo(mgItem.getItemType());
        int count = sysItemMapper.countByExample(example);
        if(count > 0){
            return "保养项目已经存在！";
        }
        mgItem.setId(CommonUtil.generateRandomNum("item-"));
        count = sysItemMapper.insertSelective(mgItem);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<SysItem> getMgItemSelectByType(String type) throws Exception {
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo(type).andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("create_time asc");
        List<SysItem> list = sysItemMapper.selectByExample(example);
        return list;
    }

    @Override
    public Map<String, List<SysItem>> getCategoryMap() throws Exception {
        Map<String, List<SysItem>> map = new HashMap<>();

        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo("1").andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("item_name asc");
        List<SysItem> list = sysItemMapper.selectByExample(example);
        for (SysItem item : list){
            String itemName = item.getItemName();
            if (map.containsKey(itemName)){
                List<SysItem> exeList = map.get(itemName);
                exeList.add(item);
            } else {
                List<SysItem> unexeList = new ArrayList<>();
                unexeList.add(item);
                map.put(itemName,unexeList);
            }
        }

        return map;
    }

    @Override
    public String addMgItemCodeDetail(SysItem mgItem) throws Exception {
        String itemCode = mgItem.getItemCode();
        String itemType = mgItem.getItemType();
        int existCount = this.countMgItemByCode(itemCode,itemType,"");
        if(existCount > 0){
            return "挤塑机号已存在！";
        }
        mgItem.setId(CommonUtil.generateRandomNum("item-"));
        int count = sysItemMapper.insertSelective(mgItem);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<SysItem> getDeviceList() throws Exception {
        return sysItemMapper.getDeviceList();
    }

    @Override
    public List<SysItem> getDeviceWorkshopList(String factory) {
        return sysItemMapper.getDeviceWorkshopList(factory);
    }

    @Override
    public Map<String, List<SysItem>> getWorkshopMap() throws Exception {
        Map<String, List<SysItem>> map = new HashMap<>();

        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo("5").andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("item_name asc");
        List<SysItem> list = sysItemMapper.selectByExample(example);
        for (SysItem item : list){
            String itemName = item.getItemName();
            if (map.containsKey(itemName)){
                List<SysItem> exeList = map.get(itemName);
                exeList.add(item);
            } else {
                List<SysItem> unexeList = new ArrayList<>();
                unexeList.add(item);
                map.put(itemName,unexeList);
            }
        }

        return map;
    }

    @Override
    public String getReferenceByWorkshop(String workshop) {
        //workshop是15-2的格式需要拆分
        String[] parts = workshop.split("-");
        //厂房
        String factory = parts[0];
        //车间
        String shop = parts[1];

        SysItemExample itemExample = new SysItemExample();
        itemExample.createCriteria().andItemNameEqualTo(factory).andItemCodeEqualTo(shop).andItemTypeEqualTo("7").andStatusEqualTo("0");
        List<SysItem> sysItems = sysItemMapper.selectByExample(itemExample);
        BigDecimal totalPrice = BigDecimal.ZERO;
        //基准节拍转换为BigDecimal
        BigDecimal decimal = new BigDecimal(sysItems.get(0).getItemPrice());

        SysDeviceExample sysDeviceExample = new SysDeviceExample();
        sysDeviceExample.createCriteria().andFactoryEqualTo(factory).andWorkshopEqualTo(shop).andStatusEqualTo("0");
        int countByExample = sysDeviceMapper.countByExample(sysDeviceExample);
        BigDecimal decimal1=new BigDecimal(countByExample);

        //基准节拍*几条产线
        BigDecimal decimal2 = decimal.multiply(decimal1);

        String priceSumStr = String.valueOf(decimal2);

        return priceSumStr;
    }

    @Override
    public String updateWorkshopDetail(SysItem mgItem) throws Exception {
        String id = mgItem.getId();
        if(StringUtils.isBlank(id)){
            return "updateMgItemDetail id null";
        }
        String itemName = mgItem.getItemName();
        String itemType = mgItem.getItemType();
        String  alias= mgItem.getItemAlias();
//        String  itemPrice= mgItem.getItemPrice();
        if(this.countMgItemByName1(itemName,alias,itemType,id) > 0){
            return "已存在该数据！";
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id);
        int count = sysItemMapper.updateByExampleSelective(mgItem,example);
        return CommonUtil.outStr(count);
    }






    private String sysItemLegal(SysItem sysItem) {

        String result = "";

        if (StringUtils.isBlank(sysItem.getItemName())){
            result = "请选择类别";
            return result;
        }
        if (StringUtils.isBlank(sysItem.getItemCode())) {
            result = "请输入物料编码";
        } else if (!sysItem.getItemCode().matches("\\d{10}")) {
            result = "物料编码必须是10位数字";
        }
        if (StringUtils.isBlank(sysItem.getItemAlias())){
            result = "请输入物料名称";
            return result;
        }
        return result;
    }


    private String sysItemProduct(SysItem sysItem) {

        String result = "";

        if (StringUtils.isBlank(sysItem.getItemName())){
            result = "请输入产品名称";
            return result;
        }
        if (StringUtils.isBlank(sysItem.getItemCode())) {
            result = "请输入物料编码";
        } else if (!sysItem.getItemCode().matches("\\d{10}")) {
            result = "物料编码必须是10位数字";
        }
        if (StringUtils.isBlank(sysItem.getItemAlias())){
            result = "请输入型号";
            return result;
        }
        if (StringUtils.isBlank(sysItem.getItemPrice())){
            result = "请输入物性";
            return result;
        }
        if (StringUtils.isBlank(sysItem.getItemTitle())){
            result = "请输入规格";
            return result;
        }
        return result;
    }

    private int countCode(String code,String type,String id) {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria();

        criteria.andStatusEqualTo(Constants.NORMAL_STATUS).andItemTypeEqualTo(type).andItemCodeEqualTo(code);
        if (StringUtils.isNotEmpty(id)){
            criteria.andIdNotEqualTo(id);
        }

        int count = sysItemMapper.countByExample(example);
        return count;
    }
}
